#! /bin/bash
### BEGIN INIT INFO
# Provides:          lance_update
# Required-Start:    $network $local_fs
# Required-Stop:     $network $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      1
# Short-Description: Start lance_update
# Description: Start and Stop the lance_update
### END INIT INFO
#
# Lance Update
#
# chkconfig: 345 20 80
# description: Lance Update
#
# /etc/rc.d/init.d/lzx*
# See how we were called.

last_rev=""
curr_rev=""
last_repo=""
last_call=""
last_update=""

HAND="[*] "
SKIP="--- "
TAB="    "
SVN="/usr/bin/svn"

function proc_pid(){
    ps -elf | grep "`basename $0`" | grep background | grep -v grep | awk '{print $4}' | head -n 1
    return 0
}

function get_url()
{
    dir_name="$1"
    $SVN info $dir_name 2>/dev/null | awk '( $1 ~ /^URL:/){print $2}'
}

function get_rev()
{
    dir_name="$1"
    $SVN info $dir_name 2>/dev/null | awk '( $1 ~ /^Revision:/){print $2}'
}

function is_svn_repo()
{
  dir_name="$1"

  $SVN info "$dir_name" 1>/dev/null 2>&1
  if [ $? -eq 1 ]
  then
    echo "false"
  else
    echo "true"
  fi

  return 0
}

function svn_log()
{
  dir_name="$1"
  curr_rev="`get_rev $dir_name`"
  if [ "$last_rev" == "$curr_rev" ]
  then
    echo "$TAB No update info [$curr_rev]."
    return 0
  fi

  echo "$TAB Log info from $last_rev to $curr_rev:"
  $SVN log -r $last_rev:$curr_rev $dir_name

  last_repo="$dir_name"
  last_update="`date`"
  return 0
}

function svn_up()
{
  dir_name="$1"
  if [ "`is_svn_repo $dir_name`" == "true" ]
  then
    last_rev="`get_rev $dir_name`"
    url="`get_url $dir_name`"
    echo "$HAND svn up \"$dir_name\""
    echo "$TAB $url"
    $SVN up "$dir_name" | sed "s/^/     /g"
    svn_log "$dir_name"
  else
    echo "$SKIP Skipped \"$dir_name\" since it is not a svn repo"
  fi
  return 0
}


function svn_up_all()
{
  echo "$TAB `date +'%Y%m%d-%H%M%S'`"
  all_dir="$1"
  find "$all_dir" -maxdepth 1 2>/dev/null | while read sub_dir
  do
    if [ -d $sub_dir ]
    then
      svn_up "$sub_dir"
    fi
  done
  return 0
}


case "$1" in
    start)
        nohup $0 background >/dev/null 2>&1 &
        PID=`proc_pid`
        echo "Started `basename $0` [$PID]"
        exit 0
    ;;

    stop)
        PID=`proc_pid`
        if [ "$PID" != "" ]
        then
            kill -s 9 $PID
            echo "Stopped `basename $0` [$PID]"
        else
            echo "`basename $0` was not running"
        fi
        exit 0
    ;;

    restart)
        $0 stop
        $0 start
        exit 0
    ;;

    test)
        echo "test begin"
        svn_up_all "/pbfd/manageability/"
        svn_up_all "/pbfd/manageability/smx/"
        echo "test end"
        exit 0
    ;;

    status)
        PID=`proc_pid`
        if [ "$PID" != "" ]
        then
            echo "`basename $0` [$PID] was running......"
        else
            echo "`basename $0` was stopped"
        fi
        exit 0
    ;;

    background)
        last_day="0"
        while [ 1 ]
        do
            # Every day
            if [ "$last_day" != "`date +'%d'`" ]
            then
              svn_up_all "/pbfd/manageability/" >> /pbfd/manageability/prepare/`basename $0`.log
              svn_up_all "/pbfd/manageability/smx/" >> /pbfd/manageability/prepare/`basename $0`.log
            fi
            last_day="`date +'%d'`"
            sleep 3600 # check one hour by one hour
        done
        exit 0
    ;;

    *)
        echo "Usage: /etc/init.d/`basename $0` {start|stop|restart|status}"
        exit 1
    ;;
esac


